package com.example.demon.wx.controllers;

import com.example.demon.entity.User;
import com.example.demon.result.CodeMsg;
import com.example.demon.result.Result;
import com.example.demon.service.UserService;
import com.sun.corba.se.spi.ior.IdentifiableFactory;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.json.JacksonJsonParser;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/wx")
public class WXController {

    @Autowired
    UserService userService;


    /**
     * 根据授权码获取Openid
     * @param code
     * @return
     */
    @GetMapping("/login")
    public Result<Object> getOpenidByCode(@RequestParam("code")String code){
        if (Strings.isEmpty(code)){
            return Result.error(50070,"授权码不能为空");
        }
        UriComponents uriComponents = UriComponentsBuilder.fromUriString("https://api.weixin.qq.com/sns/jscode2session?appid=wx198ffb3c4b1573cc&secret=27bde89d69a2307469f7b251a7fc3f7a&js_code={code}&grant_type=authorization_code")
                .buildAndExpand(code);

        RequestEntity<Void> requestEntity = RequestEntity
                .get(uriComponents.toUri())
                .headers(httpHeaders -> httpHeaders.add("User-Agent", "Mozilla/5.0"))
                .build();
        ResponseEntity<String> exchange = new RestTemplate().exchange(requestEntity, String.class);

        JacksonJsonParser jsonParser = new JacksonJsonParser();
        Map<String, Object> stringObjectMap = jsonParser.parseMap(exchange.getBody());
        
        User openid = userService.selectAUserByOpenid((String) stringObjectMap.getOrDefault("openid", ""));
        if (openid == null){
            return Result.success(CodeMsg.NOT_REGISTER,stringObjectMap.getOrDefault("openid", ""));
        }

        return Result.success(CodeMsg.SUCCESS,openid);
    }

    /**
     * 根据前端信息插入一个用户
     * @param map
     * @return
     */
    @PostMapping("/addAUser")
    public Result<Object> addOneUser(@RequestBody Map<String, String> map){
        String openid  = map.get("wxOpenid");
        String avatar  = map.get("avatar");
        String phoneNumber  = map.get("phoneNumber");
        String username  = map.get("username");
        String sex = map.get("sex");

        String input = userService.saveAUser(openid,sex,username,avatar,phoneNumber);
        if (input.equals("success")){
            //返回待定，根据业务修改
            HashMap<String,String> map1 = new HashMap<>();
            return Result.success(map1);
        }else {
            return Result.error(CodeMsg.SERVER_ERROR);
        }


    }


}
